﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;

namespace Facebook.Web.Mvc
{
    public class FbmlHelper
    {
        public String FeedForm(String templateName, String buttonLabel, params String[] fieldNames)
        {
            var formBuilder = new StringBuilder();
            var urlBuilder = new StringBuilder();

            var url = HttpContext.Current.Request.Url;
            urlBuilder.Append(url.Scheme);
            urlBuilder.Append("://");
            urlBuilder.Append(url.Host);
            if (!url.IsDefaultPort) urlBuilder.AppendFormat(":{0}", url.Port);
            urlBuilder.Append("/Feed/Post/");
            urlBuilder.Append(templateName);

            formBuilder.AppendLine("<form fbtype=\"feedStory\" action=\"" + urlBuilder.ToString() + "\">");
            foreach (var fieldName in fieldNames)
            {
                formBuilder.AppendLine("<input type=\"text\" name=\"" + fieldName + "\" />");
            }
            formBuilder.AppendLine("<input type=\"submit\" value=\"" + buttonLabel + "\" label=\"" + buttonLabel + "\" />");
            formBuilder.AppendLine("</form>");

            return formBuilder.ToString();
        }

        public String RequireSession(String path)
        {
            path = path.Split('?')[0];
            var script = new StringBuilder();
            script.AppendLine("var session = FB.Facebook.apiClient.get_session();");
            script.AppendLine(String.Format("var url = '{0}' + '?fb_sig_added=1'", path)); 
            script.AppendLine("for(var key in session) url += '&'+'fb_sig_'+key+'='+session[key];");
            script.AppendLine("window.location.href = url;");
            return script.ToString();
        }

        public String ActionLink(String path, String text, Boolean requireSession, String extendedPermission)
        {
            var link = new StringBuilder("<a href=\"" + path + "\"");
            link.AppendFormat(path);

            if (requireSession)
            {
                var url = path + (path.IndexOf('?') < 0 ? "?" : "&") + "fb_sig_added=1";
                var redirectCallback = String.Format("function(){{{0}}}", this.RequireSession(path));
                String callback = null;
                if (!String.IsNullOrEmpty(extendedPermission)) callback = String.Format("function(){{FB.Connect.showPermissionDialog('{0}',{1});}}", extendedPermission, redirectCallback);
                else callback = redirectCallback;

                link.AppendFormat(" onclick=\"FB.Connect.requireSession({0});return false;\"", callback);
            }

            link.Append(">");
            link.Append(text);
            link.Append("</a>");

            return link.ToString();
        }

        public String ActionLink(String path, String text)
        {
            return this.ActionLink(path, text, false, null);
        }

        public String ActionLink(String path, String text, Boolean requireSession)
        {
            return this.ActionLink(path, text, requireSession, null);
        }

        public String ActionLink(String path, String text, String extendedPermission)
        {
            return this.ActionLink(path, text, true, extendedPermission);
        }
    }
}